<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>采购退货订单仓库处理</title>
<script type="text/javascript">
    var returnedDeliveryWin;

    var purchaseReturnedId;
    var currentOrderNumber;//当前正操作的采购单单号
    Ext.onReady(function () {

        var orderNumber = Ext.create('Ext.form.field.Text', {
            fieldLabel: '单号',
            name: 'order_number'
        });

        var vendor = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '供应商',
            store: vendorStore,
            valueField: 'id',
            displayField: 'name',
            name: 'vendor_id',
            loadingText: '搜索中...',
            //pageSize: 10,
            minChars: 0,
            editable: false
        });

        var beginDate = Ext.create('Ext.form.field.Date', {
            fieldLabel: '退货日期',
            name: 'begin_date',
            id: 'begin_date',
            format: 'Y-m-d', //格式化日期
            width: '100',
            //disabledDays: [0,6],	//不让选择周六，周日
            //disabledDaysText: "周末要休息",
            invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
            vtype: 'daterange',
            endDateField: 'end_date'
        });

        var endDate = Ext.create('Ext.form.field.Date', {
            fieldLabel: '至    ',
            name: 'end_date',
            id: 'end_date',
            format: 'Y-m-d', //格式化日期
            //disabledDays: [0,6],	//不让选择周六，周日
            //disabledDaysText: "周末要休息",
            invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
            vtype: 'daterange',
            startDateField: 'begin_date'
        });

        var submitButton = Ext.create('Ext.Button', {
            text: '搜索',
            iconCls: 'finding',
            handler: function () {
                purchaseReturnedDS.removeAll();
                purchaseReturnedDS.load({params: {
                    'order_number': orderNumber.getValue(),
                    'vendor_id': vendor.getValue(),
                    'begin_date': beginDate.getRawValue(),
                    'end_date': endDate.getRawValue(),
                    'search_type': 'warehouse'
                }});
            }
        });

        var clearButton = Ext.create('Ext.Button', {
            text: '清空',
            width: 50,
            handler: function () {
                queryFormPanel.form.reset();
            }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
            fieldDefaults: {
                labelWidth: 65,
                labelAlign: 'right'
            },
            width: screenWidth - 165,
            frame: true,
            border: false,
            items: [
                {
                    xtype: 'fieldset',
                    defaults: {width: 800},
                    title: '查询条件',
                    border: true,
                    items: [
                        {
                            layout: 'column',
                            xtype: 'container',
                            items: [
                                {
                                    columnWidth: .3,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [orderNumber]
                                },
                                {
                                    columnWidth: .5,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [vendor]
                                }
                            ]
                        },
                        {
                            layout: 'column',
                            xtype: 'container',
                            items: [
                                {
                                    columnWidth: .3,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [beginDate]
                                },
                                {
                                    columnWidth: .3,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [endDate]
                                },
                                {
                                    columnWidth: .15,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [submitButton]
                                },
                                {
                                    columnWidth: .15,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [clearButton]
                                }
                            ]
                        }
                    ]
                }
            ]
        });
        //显示采购退货订单操作时的窗口介面。
        //----------------------------------采购退货订单Grid-------------------------
        //采购退货订单Grid Store
        var purchaseReturnedDS = Ext.create('Ext.data.Store', {
            //autoDestroy : true,
            model: 'PurchaseReturnedModel',
            proxy: {
                type: 'ajax',
                url: '/purchase_returned/getAllPurchaseReturneds',
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty: 'totalProperty'// 数据集记录总数
                }
            },
            pageSize: pageSize,
            autoLoad: true
        });

        //生成要订单的产品的Grid
        var purchaseReturnedGrid = Ext.create('Ext.grid.Panel', {
            title: '采购退货订单列表',
            store: purchaseReturnedDS,
            columns: [Ext.create('Ext.grid.RowNumberer'), {
                header: 'DB',
                dataIndex: 'id',
                width: 50
            }, {
                header: '公司',
                dataIndex: 'company_name',
                width: 50
            }, {
                header: '采购退货订单单号',
                dataIndex: 'order_number',
                width: 120
            }, {
                header: '采购订单单号',
                dataIndex: 'purchase_order_number',
                width: 120
            }, {
                header: '供应商',
                dataIndex: 'vendor_name',
                width: 130
            }, {
                header: '总金额',
                dataIndex: 'amount',
                align: 'right',
                width: 75
            }, {
                header: '已付金额',
                dataIndex: 'paid_amount',
                align: 'right',
                summaryType: 'sum',
                width: 75
            }, {
                header: '退货日期',
                dataIndex: 'return_date',
                renderer: Ext.util.Format.dateRenderer('Y-m-d'),
                width: 80
            }, {
                header: '退货出库日期',
                dataIndex: 'action_date',
                renderer: Ext.util.Format.dateRenderer('Y-m-d'),
                width: 85
            }, {
                header: '操作员',
                dataIndex: 'employee_name',
                width: 60
            }, {
                header: '部门',
                dataIndex: 'department_name',
                width: 70
            }, {
                header: '审批状态',
                dataIndex: 'auditing_status',
                width: 60,
                renderer: auditingStatusRender
            }, {
                header: '内勤审批者',
                dataIndex: 'associate_auditor_name',
                width: 75
            }, {
                header: '内勤审批意见',
                dataIndex: 'associate_audit_opinion',
                width: 150
            }, {
                header: '处理状态',
                dataIndex: 'deal_with_status',
                renderer: dealWithStatusRender,
                width: 70
            }, {
                header: '出库状态',
                dataIndex: 'store_status',
                renderer: dealWithStatusRender,
                width: 70
            }, {
                header: '仓库操作员',
                dataIndex: 'delivery_employee_name',
                width: 70
            }, {
                header: '生成日期',
                dataIndex: 'created_at',
                renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
                width: 100
            }, {
                header: '修改日期',
                dataIndex: 'updated_at',
                renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
                width: 100
            }],
            selModel: Ext.create('Ext.selection.CheckboxModel'),
            loadMask: true,
            width: screenWidth - 165,
            height: 320,
            frame: true,
            //title: '进货品种列表',
            //plain:true,
            //plugins: checkColumn,
            bbar: Ext.create('Ext.toolbar.Paging', {
                pageSize: pageSize,
                store: purchaseReturnedDS,
                displayInfo: true,
                displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
                emptyMsg: "没有记录"
            })
        });


        purchaseReturnedGrid.on('itemdblclick', function (gridPanel, record, item, index, e, options) {
            purchaseReturnedId = purchaseReturnedDS.getAt(index).get('id');
            var purchaseReturnedNumber = purchaseReturnedDS.getAt(index).get('order_number');
            //purchaseReturnedDS.load();
            purchaseReturnedDetailDS.removeAll();
            purchaseReturnedDetailDS.load({params: {'purchase_returned_id': purchaseReturnedId}});
            purchaseReturnedDetailGrid.setTitle("采购退货订单：" + purchaseReturnedNumber + "，的详细信息");
        });

        //右键菜单，以弹出此订单相关的入库单
        //右键
        purchaseReturnedGrid.on('itemcontextmenu', function (grid, record, item, index, e, options) {
            purchaseReturnedId = purchaseReturnedDS.getAt(index).get('id');
            currentOrderNumber = purchaseReturnedDS.getAt(index).get('order_number');
            var auditingStatus = purchaseReturnedDS.getAt(index).get('auditing_status');
            returnedDeliveryFinishItem.setDisabled(true);
            if (auditingStatus == "通过退货") {
                returnedDeliveryFinishItem.setDisabled(false);
            }
            e.preventDefault();
            rightClick.showAt(e.getXY());
        });

        //############ 订单-右键菜单 ##########
        var returnedDeliveryFinishItem = Ext.create('Ext.menu.Item', {
            text: '【采购退货出库完毕】',
            //iconCls: 'album',
            handler: rightMenuReturnedDeliveryFn
        });

        function rightMenuReturnedDeliveryFn() {
            returnedDeliveryWin = Ext.create('Ext.window.Window', {
                layout: 'fit',
                width: 307,
                height: 133,
                closeAction: 'destroy',
                plain: true,
                modal: true,
                constrain: true,
                //modal: true,
                title: '采购退货出库完毕确认',
                autoLoad: {
                    url: "/purchase_returned/deliveryConfirm?id=" + purchaseReturnedId,
                    scripts: true,
                    callback: function (options, success, response, request) {
                        if (response.responseText === "NoPrivilege") {
                            returnedDeliveryWin.close();
                        }
                    }
                }
            });
            returnedDeliveryWin.on("destroy", function () {
                purchaseReturnedDS.load();
            });
            returnedDeliveryWin.resizable = false;
            returnedDeliveryWin.show();
        }

        //############ 订单-右键菜单 ##########
        //这块操作，暂时不作。
        var cancelReturnedDeliveryFinishItem = Ext.create('Ext.menu.Item', {
            text: '【撤消采购退货出库】',
            //iconCls: 'album',
            handler: rightMenuCancelReturnedDeliveryFn
        });

        function rightMenuCancelReturnedDeliveryFn() {
            returnedDeliveryWin = Ext.create('Ext.window.Window', {
                layout: 'fit',
                width: 307,
                height: 107,
                closeAction: 'destroy',
                plain: true,
                modal: true,
                constrain: true,
                //modal: true,
                title: '采购退货出库完毕确认',
                autoLoad: {
                    url: "/purchase_returned/deliveryConfirm?id=" + purchaseReturnedId,
                    scripts: true,
                    callback: function (options, success, response, request) {
                        if (response.responseText === "NoPrivilege") {
                            returnedDeliveryWin.close();
                        }
                    }
                }
            });
            returnedDeliveryWin.on("destroy", function () {
                purchaseReturnedDS.load();
            });
            returnedDeliveryWin.resizable = false;
            returnedDeliveryWin.show();
        }


        var rightClick = Ext.create('Ext.menu.Menu', {
            items: [returnedDeliveryFinishItem]
        });


        //--------------------------采购退货订单详细，双击后可显示数据----------------
        //采购退货订单Grid Store Detail
        var purchaseReturnedDetailDS = Ext.create('Ext.data.Store', {
            //autoDestroy : true,
            model: 'PurchaseReturnedDetailModel',
            proxy: {
                type: 'ajax',
                url: '/purchase_returned/getPurchaseReturnedDetailsById',
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty: 'totalProperty'// 数据集记录总数
                }
            }
        });

        //生成要订单的产品的Grid
        var purchaseReturnedDetailGrid = Ext.create('Ext.grid.Panel', {
            title: '订单详细列表',
            store: purchaseReturnedDetailDS,
            columns: [Ext.create('Ext.grid.RowNumberer'), {
                header: 'DB',
                dataIndex: 'id',
                width: 50
            }, {
                header: '产品名称',
                dataIndex: 'product_name',
                width: 200
            }, {
                header: '编号', dataIndex: 'product_number', width: 100
            }, {
                header: '规格',
                dataIndex: 'spec',
                width: 80
            }, {
                header: '数量',
                dataIndex: 'quantity',
                width: 80
            }, {
                header: '单价',
                dataIndex: 'unit_price',
                align: 'right',
                renderer: moneyPrivilegeRender,
                width: 70
            }, {
                header: '总价',
                dataIndex: 'amount',
                align: 'right',
                renderer: moneyPrivilegeRender,
                width: 70
            }, {
                header: '套装或单品',
                dataIndex: 'is_suit',
                width: 70
            }, {
                header: '备注',
                dataIndex: 'description',
                width: 150
            }],
            loadMask: true,
            width: screenWidth - 165,
            height: dgHeight,
            frame: true
        });

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
            width: screenWidth - 165,
            height: totalHeight,
            frame: false,

            layout: {type: 'table', columns: 1},

            items: [queryFormPanel, purchaseReturnedGrid, purchaseReturnedDetailGrid]
        });
        workbenchPanel.render("workbench_panel");
    })
</script>
</head>
<body>
<div id="workbench_panel"></div>
</body>
</html>